<?php

namespace adminApi\modules\baseData\controllers;

use common\components\fast_api\Controller;
use common\components\wtools\tools\Model;
use common\datas\Enums;
use common\models\db\BaseAreas;
use yii\base\Exception;

class AreasController extends Controller
{
    /**
     * 获取节点下的地区
     * @desc 一般在下拉选择ajax使用
     * @param int $pid
     * @return array
     * @return array list
     */
    public function actionTableSelect($pid = BaseAreas::ID_CHINA)
    {
        $query = BaseAreas::find()
            ->with(['parent', 'children'])
            ->orderBy(['id' => SORT_ASC]);
        // 去除港澳台
        $query->andWhere(['<', 'id', 700000000000]);
        if ($pid > 0) {
            $query->andWhere(['pid' => $pid]);
        } else {
            $query->andWhere(['pid' => null]);
        }
        $categories = $query->all();
        $list = [];
        foreach ($categories as $k => &$v) {
            $m = [];
            $m['id'] = $v->id;
            $m['level'] = $v->level;
            $m['name'] = $v->name;
            $m['mergerName'] = $v->merger_name;
            $m['prefix'] = $v->prefix;
            $m['zipCode'] = $v->zip_code;
            $m['pid'] = 0;
            if (in_array($v->level, [BaseAreas::LEVEL_1, BaseAreas::LEVEL_2])) {
                if ($v->children) $m['children'] = [];

            }
            if (in_array($v->level, [BaseAreas::LEVEL_2, BaseAreas::LEVEL_3])) {
                $m['pid'] = $v->pid;
                $m['pName'] = $v->parent->name;
            }
            $list[] = $m;
        }
        return $this->apiSuccess([
            'list' => $list,
        ]);
    }

    /**
     * 添加或编辑地区
     * @param string $name
     * @param int $pid
     * @param int $level
     * @param int $id
     * @return array|Exception
     * @throws Exception
     */
    public function actionAddOrEdit($id, $name, $pid = 0, $level = BaseAreas::LEVEL_3, $isAdd = Enums::YES)
    {
        if ($isAdd == Enums::YES) {
            $baseArea = new BaseAreas();
        } else {
            $baseArea = BaseAreas::findOne($id);
            if (!$baseArea) {
                throw new Exception('地区{$id}不存在');
            }
        }
        $baseArea->id = $id;
        $baseArea->name = $name;
        if($pid > 0) {
            $baseArea->pid = $pid;
        }
        $baseArea->level = $level;
        if (!$baseArea->save()) {
            return $this->apiError(Model::getModelError($baseArea));
        }
        $m = [];
        $m['id'] = $baseArea->id;
        $m['level'] = $baseArea->level;
        $m['name'] = $baseArea->name;
        $m['mergerName'] = $baseArea->merger_name;
        $m['prefix'] = $baseArea->prefix;
        $m['zipCode'] = $baseArea->zip_code;
        $m['pid'] = $baseArea->pid;
        $m['pName'] = $baseArea->parent->name;
        return $this->apiSuccess([
            'baseArea' => $m,
        ]);
    }

    /**
     * 删除地区
     * @desc 暂不使用
     * @param int $id
     * @return array
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDel($id)
    {
        $baseArea = BaseAreas::findOne($id);
        if (!$baseArea) {
            return $this->apiError('地区不存在');
        }
        if ($baseArea->children) {
            return $this->apiError('该地区下有子地区，不能删除');
        }
        if (!$baseArea->delete()) {
            return $this->apiError(Model::getModelError($baseArea));
        }
        return $this->apiSuccess();
    }

    /**
     * 获取省
     * @desc 在下拉选择ajax使用
     * @return array
     * @return array list
     */
    public function actionProvinceSelect()
    {
        $query = BaseAreas::find();
        $query->andWhere(['pid' => BaseAreas::ID_CHINA]);
        // 去除港澳台
        $query->andWhere(['<', 'id', 700000000000]);
        $areas = $query->all();
        $list = [];
        foreach ($areas as $k => &$v) {
            $m = [];
            $m['value'] = $v->id;
            $m['label'] = $v->name;
            $list[] = $m;
        }
        return $this->apiSuccess([
            'list' => $list,
        ]);
    }

    /**
     * 获取子地区
     * @desc 在下拉选择ajax使用
     * @param int $pid
     * @return array
     * @return array list
     */
    public function actionSelect($pid = BaseAreas::ID_CHINA)
    {
        $query = BaseAreas::find();
        if ($pid > 0) {
            $query->andWhere(['pid' => $pid]);
        } else {
            $query->andWhere(['pid' => null]);
        }
        // 去除港澳台
        $query->andWhere(['<', 'id', 700000000000]);
        $areas = $query->all();
        $list = [];
        foreach ($areas as $k => &$v) {
            $m = [];
            $m['id'] = $v->id;
            $m['value'] = $v->id;
            $m['title'] = $v->name;
            $m['pid'] = $v->pid;
            $m['isLeaf'] = true;
            if ($v->children && in_array($v->level, [BaseAreas::LEVEL_1, BaseAreas::LEVEL_2])) {
                $m['isLeaf'] = false;
            }
            $list[] = $m;
        }
        return $this->apiSuccess([
            'list' => $list,
        ]);
    }
}
